//
//  CHScreenView.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/3.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHScreenView.h"
#import "CHSectionViewCell.h"
#import "PJ_Model.h"
@interface CHScreenView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) NSMutableArray *selectArr;
@end
@implementation CHScreenView
- (NSMutableArray *)selectArr{
    if (!_selectArr) {
        _selectArr = [NSMutableArray array];
    }
    return _selectArr;
}


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithHexString:@"f6f6f6" alpha:1.0f];
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    UIImageView *iconImgView=  [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 5, 20)];
    iconImgView.image = [UIImage imageNamed:@"txt_bor"];
    [self addSubview:iconImgView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iconImgView.frame) +5, 10, 100, 30)];
    titleLabel.text = @"企业筛选";
    titleLabel.textColor = [UIColor colorWithHexString:@"222222" alpha:1.0f];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:titleLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame)+5, UIScreenW, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:0.3];
    [self addSubview:line];
    
    UIButton *allBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(line.frame) +15, 100, 25)];
    [allBtn setTitle:(NSString *)[Util getValue:@"areacn"] forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [allBtn setTitleColor:[UIColor colorWithHexString:@"666666" alpha:1.0f] forState:UIControlStateNormal];
    allBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    allBtn.layer.cornerRadius = 25/2;
    allBtn.layer.masksToBounds = YES;
    allBtn.layer.borderWidth = 1;
    allBtn.layer.borderColor = [UIColor colorWithHexString:@"e5e5e5" alpha:1.0f].CGColor;
    [allBtn addTarget:self action:@selector(allBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:allBtn];
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.itemSize = CGSizeMake((UIScreenW -20*5) /4, 25);
    UICollectionView *collectionView =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(allBtn.frame) +10, UIScreenW,self.h - CGRectGetMaxY(allBtn.frame) +10) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.scrollEnabled = NO;
    collectionView.pagingEnabled = YES;
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerClass:[CHSectionViewCell class] forCellWithReuseIdentifier:@"CHSectionViewCell"];
    [self addSubview:collectionView];
    self.collectionView = collectionView;

    
    
}


-(void)allBtnClick:(UIButton *)btn{
    btn.highlighted = NO;
    btn.selected = !btn.selected;
    if (btn.selected) {
        btn.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        [self.selectArr addObject:(NSString *)[Util getValue:@"areaid"]];
    }else{
        btn.backgroundColor = [UIColor clearColor];
        [self.selectArr removeObject:(NSString *)[Util getValue:@"areaid"]];
    }
    
    if ([self.delegate respondsToSelector:@selector(didClickItemWithSelectedArr:)]) {
        [self.delegate didClickItemWithSelectedArr:self.selectArr];
    }
}


#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  [Globle getInstance].datas.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"CHSectionViewCell";
    CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    PJ_Model *model =[Globle getInstance].datas[indexPath.row];
    cell.layer.cornerRadius = 25/2;
    cell.layer.masksToBounds = YES;
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [UIColor colorWithHexString:@"e5e5e5" alpha:1.0f].CGColor;
    cell.label.text = model.areacn;
    cell.label.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0f];
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 , 20, 10,20);
}

-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{
    return 15;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
  
    NSLog(@"点击了第%ld组的第%ld个", indexPath.section, indexPath.row);
    CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.isSelected = !cell.isSelected;
    PJ_Model *model =[Globle getInstance].datas[indexPath.row];
    if (cell.isSelected) {
        [self.selectArr addObject:model.areaid];
        cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        cell.label.textColor = [UIColor whiteColor];
    }else{
        
        [self.selectArr removeObject:model.areaid];
        cell.backgroundColor = [UIColor clearColor];
        cell.label.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0f];

    }
    
    if ([self.delegate respondsToSelector:@selector(didClickItemWithSelectedArr:)]) {
        [self.delegate didClickItemWithSelectedArr:self.selectArr];
    }

}




@end
